from flask_login import current_user
from flask_restful import Resource, marshal_with,reqparse

from core.roles.role_check import check_perms
from libs.helper import StrBoolNone
from libs.login import login_required
from controllers.console import api
from fields.online_mall.transport_fields_def import transport_fields,transport_list_fields
from services.online_mall.transport_service import TransportTemService


class TransportTempApi(Resource):
    @login_required
    @marshal_with(transport_list_fields)
    @check_perms(perms=["mall:transport:list"])
    def get(self):
        parser= reqparse.RequestParser()
        parser.add_argument("name",type=str,required=False,location='args')
        parser.add_argument("shop_id",type=str,required=False,location='args')
        parser.add_argument("charge_type",type=str,required=False,location='args')
        parser.add_argument("is_free_fee",type=StrBoolNone,required=False,location='args')
        parser.add_argument("has_free_condition",type=StrBoolNone,required=False,location='args')
        parser.add_argument("page",type=int,required=False,default=-1,location='args')
        parser.add_argument("page_size",type=int,required=False,default=-1,location='args')
        args = parser.parse_args()
        return TransportTemService.get_list(current_user,args)
    
    @login_required
    @marshal_with(transport_fields)
    @check_perms(perms=["mall:transport:create"])
    def post(self):
        parser= reqparse.RequestParser()
        parser.add_argument("name",type=str,required=False,location='json')
        parser.add_argument("shop_id",type=str,required=True,location='json')
        parser.add_argument("charge_type",type=str,required=False,location='json')
        parser.add_argument("is_free_fee",type=StrBoolNone,required=False,location='json')
        parser.add_argument("has_free_condition",type=StrBoolNone,required=False,location='json')
        parser.add_argument("transfee_frees",type=dict,action='append',required=False,default=[],location='json')
        parser.add_argument("transfees",type=dict,action='append',required=False,default=[],location='json')
        args = parser.parse_args()
        return TransportTemService.create_temp(current_user,args)
    
    @login_required
    @marshal_with(transport_fields)
    @check_perms(perms=["mall:transport:update"])
    def put(self):
        parser= reqparse.RequestParser()
        parser.add_argument("id",type=str,required=True,location='json')
        parser.add_argument("name",type=str,required=False,location='json')
        parser.add_argument("shop_id",type=str,required=True,location='json')
        parser.add_argument("charge_type",type=str,required=False,location='json')
        parser.add_argument("is_free_fee",type=StrBoolNone,required=False,location='json')
        parser.add_argument("has_free_condition",type=StrBoolNone,required=False,location='json')
        parser.add_argument("transfee_frees",type=dict,action='append',required=False,default=[],location='json')
        parser.add_argument("transfees",type=dict,action='append',required=False,default=[],location='json')
        args = parser.parse_args()
        return TransportTemService.update_temp(current_user,args)
    
    @login_required
    @marshal_with(transport_fields)
    @check_perms(perms=["mall:transport:del"])
    def delete(self):
        parser= reqparse.RequestParser()
        parser.add_argument("id",type=str,required=True,location='json')
        args = parser.parse_args()
        TransportTemService.delete_temp(current_user,args)
        return {
            "code":200,
            "message":"success"
        }

class TransportTempInfoApi(Resource):

    @login_required
    @marshal_with(transport_fields)
    @check_perms(perms=["mall:transport:info"])
    def get(self,trans_id:str):
        return TransportTemService.get_by_id(current_user,trans_id)
    
api.add_resource(TransportTempInfoApi,'/transports/<string:trans_id>')
api.add_resource(TransportTempApi,'/transports')